package com.ruoyi.product.service.impl;

import java.util.List;

import com.ruoyi.product.domain.*;
import com.ruoyi.product.mapper.ProductActivityCategorySkuRelationMapper;
import com.ruoyi.product.mapper.ProductActivityMapper;
import com.ruoyi.product.service.IProductActivityCategorySkuRelationService;
import com.ruoyi.product.service.IProductImageService;
import com.ruoyi.product.service.IProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.ProductActivityCategoryMapper;
import com.ruoyi.product.service.IProductActivityCategoryService;

/**
 * 活动自定义分类Service业务层处理
 *
 * @author liuhuizhong
 * @date 2025-08-13
 */
@Service
public class ProductActivityCategoryServiceImpl implements IProductActivityCategoryService {
    @Autowired
    private ProductActivityCategoryMapper productActivityCategoryMapper;
    @Autowired
    private ProductActivityMapper productActivityMapper;
    @Autowired
    private IProductActivityCategorySkuRelationService productActivityCategorySkuRelationService;
    @Autowired
    private IProductSkuService productSkuService;
    @Autowired
    private IProductImageService productImageService;

    /**
     * 查询活动自定义分类
     *
     * @param customCategoryId 活动自定义分类主键
     * @return 活动自定义分类
     */
    @Override
    public ProductActivityCategory selectProductActivityCategoryByCustomCategoryId(Long customCategoryId) {
        ProductActivityCategory productActivityCategory = productActivityCategoryMapper.selectProductActivityCategoryByCustomCategoryId(customCategoryId);
        if (productActivityCategory != null) {
            productActivityCategory.setActivity(productActivityMapper.selectProductActivityByActivityId(productActivityCategory.getActivityId()));
            List<ProductActivityCategorySkuRelation> skuRelations = productActivityCategorySkuRelationService.selectRelationListByCategoryId(customCategoryId);
            if (skuRelations != null) {
                for (ProductActivityCategorySkuRelation skuRelation : skuRelations) {
                    ProductSkuVo sku = productSkuService.selectProductSkuById(skuRelation.getSkuId());
                    sku.setProductImages(productImageService.selectProductImageList(new ProductImage() {
                        @Override
                        public Long getSkuId() {
                            return sku.getId();
                        }
                    }));
                    skuRelation.setSku(sku);
                }
            }
            productActivityCategory.setSkuRelations(skuRelations);
        }
        return productActivityCategory;
    }

    /**
     * 查询活动自定义分类列表
     *
     * @param activityId 活动id
     * @return 活动自定义分类
     */
    public List<ProductActivityCategory> getActivityCategoryListByActivityId(Long activityId) {
        List<ProductActivityCategory> list = productActivityCategoryMapper.getActivityCategoryList(activityId);
        setCategoryDetail(activityId, list);
        return list;
    }

    private void setCategoryDetail(Long activityId, List<ProductActivityCategory> list) {
        for (ProductActivityCategory activityCategory : list) {
            activityCategory.setActivity(productActivityMapper.selectProductActivityByActivityId(activityId));
            List<ProductActivityCategorySkuRelation> skuRelations = productActivityCategorySkuRelationService.selectRelationListByCategoryId(activityCategory.getCustomCategoryId());
            if (skuRelations != null) {
                for (ProductActivityCategorySkuRelation skuRelation : skuRelations) {
                    ProductSkuVo sku = productSkuService.selectProductSkuById(skuRelation.getSkuId());
                    sku.setProductImages(productImageService.selectProductImageList(new ProductImage() {
                        @Override
                        public Long getSkuId() {
                            return sku.getId();
                        }
                    }));
                    skuRelation.setSku(sku);
                }
                activityCategory.setSkuRelations(skuRelations);
            }
        }
    }

    /**
     * 查询活动自定义分类列表
     *
     * @param productActivityCategory 活动自定义分类
     * @return 活动自定义分类
     */
    @Override
    public List<ProductActivityCategory> selectProductActivityCategoryList(ProductActivityCategory productActivityCategory) {
        List<ProductActivityCategory> list = productActivityCategoryMapper.selectProductActivityCategoryList(productActivityCategory);
        setCategoryDetail(productActivityCategory.getActivityId(), list);
        return list;
    }

    /**
     * 新增活动自定义分类
     *
     * @param productActivityCategory 活动自定义分类
     * @return 结果
     */
    @Override
    public int insertProductActivityCategory(ProductActivityCategory productActivityCategory) {
        return productActivityCategoryMapper.insertProductActivityCategory(productActivityCategory);
    }

    /**
     * 修改活动自定义分类
     *
     * @param productActivityCategory 活动自定义分类
     * @return 结果
     */
    @Override
    public int updateProductActivityCategory(ProductActivityCategory productActivityCategory) {
        return productActivityCategoryMapper.updateProductActivityCategory(productActivityCategory);
    }

    /**
     * 批量删除活动自定义分类
     *
     * @param customCategoryIds 需要删除的活动自定义分类主键
     * @return 结果
     */
    @Override
    public int deleteProductActivityCategoryByCustomCategoryIds(Long[] customCategoryIds) {
        return productActivityCategoryMapper.deleteProductActivityCategoryByCustomCategoryIds(customCategoryIds);
    }

    /**
     * 删除活动自定义分类信息
     *
     * @param customCategoryId 活动自定义分类主键
     * @return 结果
     */
    @Override
    public int deleteProductActivityCategoryByCustomCategoryId(Long customCategoryId) {
        return productActivityCategoryMapper.deleteProductActivityCategoryByCustomCategoryId(customCategoryId);
    }
}
